import os
import glob
import libtcodpy as libtcod

import menu
import splash
import pickle

class HallOfFame(object):
    def __init__(self, exit_offset, jukebox):
        self.exit_offset = exit_offset
        self.sounds = dict(denied=jukebox.event_song('bad_end'),
                           accept=jukebox.event_song('start'),
                           query=jukebox.event_song('save'),
                          ) 
        self.song_count = len(glob.glob(os.path.join('songs', '*.dat')))

    def retire(self, con, player):
        if len(player.gigs) < self.song_count:
            needed = self.song_count - len(player.gigs)
            text = "Sorry, you need to complete %d more songs before you can enter the Hall of Fame." % needed
            width, height = con.width / 2, con.height / 2
            msg = splash.Splash(text, width=width, height=height, title='Denied', 
                                border=True, wrap=True, background_alpha=0.8)
            if self.sounds['denied'] is not None: self.sounds['denied'].play()
            msg.show(con, (con.width - width) / 2, (con.height - height) / 2)
        else:
            if self.sounds['query'] is not None: self.sounds['query'].play()

            items = ['No', 'Yes']
            items_width, items_height = con.width / 2, 5
            select_x, select_y = (con.width - items_width) / 2, 10

            select = menu.Menu(items_width, items_height, 'Really retire?', items) 
            item = select.show(con, select_x, select_y, background_alpha=0.8)

            if (item is not None) and items.index(item):
                if self.sounds['accept'] is not None: self.sounds['accept'].play()
                player.done = 'Retired'

        player.x += self.exit_offset[0]
        player.y += self.exit_offset[1]


def exit(reason, exit_offset=None, music=None):
    if exit_offset is None:
        exit_offset = (0, 0)

    def confirm(console, player):
        if music is not None:
            music.play()
        items = ['No', 'Yes']
        items_width, items_height = console.width / 2, 5
        select_x, select_y = (console.width - items_width) / 2, 10

        select = menu.Menu(items_width, items_height, 'Really Quit?', items) 
        item = select.show(console, select_x, select_y, background_alpha=0.8)

        if (item is not None) and items.index(item):
            player.done = reason

        player.x += exit_offset[0]
        player.y += exit_offset[1]

    return confirm

def save(save_file, exit_offset=None, music=None):
    if exit_offset is None:
        exit_offset = (0, 0)

    def confirm(console, player):
        if music is not None:
            music.play()
        items = ['No', 'Yes']
        items_width, items_height = console.width / 2, 5
        select_x, select_y = (console.width - items_width) / 2, 10

        select = menu.Menu(items_width, items_height, 'Save and quit?', items) 
        item = select.show(console, select_x, select_y, background_alpha=0.8)

        player.x += exit_offset[0]
        player.y += exit_offset[1]

        if items.index(item):
            # Okay, I'm really sorry about this.  Careful when loading
            # savegames of dubious origin.

            fp = open(save_file, 'wb')
            pickle.dump(player, fp, pickle.HIGHEST_PROTOCOL)
            fp.close()
            player.done = 'Saved'

    return confirm





        
        




        
        
